package com.lill.org.api.action;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lill.common.component.util.R;
import com.lill.org.model.CompanyArchitectureModel;
import com.lill.org.model.EmployeeModel;
import com.lill.org.service.ICompanyArchitectureService;
import com.lill.org.service.IEmployeeService;

@Api(value="api/org/employee", tags="员工接口")
@RestController
@RequestMapping("api/org/employee")
public class EmployeeInfoAction {
	
	@Autowired
	private IEmployeeService employeeService;
	@Autowired
    private ICompanyArchitectureService architectureService;
	/**
	 * 获取门店下的所有人员
	 * @param branchId 门店id
	 * @return
	 */
	@ApiOperation(value="获取门店下所有人员列表",notes="获取门店下所有人员列表",httpMethod="GET")
	@GetMapping("/listAllByBranchId")
	public R employeeList(@RequestParam String branchId) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("branchAll", branchId);
		return R.ok().put("data", employeeService.queryList(params));
	}
	
	/**
	 * 获取部门下的直接人员
	 * @param branchId 门店id
	 * @return
	 */
	@ApiOperation(value="获取部门下的直接人员",notes="获取部门下的直接人员",httpMethod="GET")
	@GetMapping("/listByBranchId")
	public R employeeDepartList(@RequestParam String companyId,@RequestParam String departId) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("departId", departId);
		return R.ok().put("data", employeeService.queryList(params));
	}
	
	/**
	 * 获取门店下的所有部门
	 * @param branchId 门店id
	 * @return
	 */
	@ApiOperation(value="获取门店下的所有部门",notes="获取门店下的所有部门",httpMethod="GET")
	@GetMapping("/listByDepartId")
	public R departList(@RequestParam String branchId) {
		Map<String, Object> params=new HashMap<String, Object>();
    	params.put("parentList", branchId);
    	params.put("ownId", branchId);
		return R.ok().put("data", architectureService.queryList(params));
	}


	@ApiOperation(value="获取门店下的部门以及部门中的人",notes="获取门店下的部门以及部门中的人")
	@PostMapping("/listByBranchAndEmployee")
	public R listByBranchAndEmployee(@RequestBody Map<String,Object> map) {
		if (
				!map.containsKey("companyId") || map.get("companyId") == null || "".equals(map.get("companyId") + "")
				|| !map.containsKey("branchId") || map.get("branchId") == null || "".equals(map.get("branchId") + "")
		) {
			return R.error("参数异常branchId,请联系管理员");
		}
		Object branchId = map.get("branchId");
		Object companyId = map.get("companyId");
		Map<String, Object> params=new HashMap<String, Object>();
    	params.put("parentList", branchId);
    	params.put("ownId", branchId);
		List<CompanyArchitectureModel> companyArchitectureModels = architectureService.queryList(params);
		List<Map<String,Object>>savemList = new ArrayList<>();
		if (companyArchitectureModels != null && companyArchitectureModels.size() > 0 ) {
			for (int i = 0; i < companyArchitectureModels.size(); i++) {
				CompanyArchitectureModel companyArchitectureModel = companyArchitectureModels.get(i);
				if ("D".equals(companyArchitectureModel.getType())) {
					Map<String,Object>saveMap = new HashMap<>();
					saveMap.put("id",companyArchitectureModel.getId());
					saveMap.put("name",companyArchitectureModel.getName());
					// 查询门店下的人
					Map<String,Object>queEmp = new HashMap<>();
					queEmp.put("companyId", companyId);
					queEmp.put("departId", companyArchitectureModel.getId());
					List<EmployeeModel> employeeModels = employeeService.queryList(queEmp);
					List<Map<String,Object>> emList = new ArrayList<>();
					if (employeeModels != null && employeeModels.size() > 0 ) {
						for (int j = 0; j < employeeModels.size(); j++) {
							Map<String,Object> empMap = new HashMap<>();
							EmployeeModel employeeModel = employeeModels.get(j);
							empMap.put("employeeId",employeeModel.getId());
							empMap.put("employeeName",employeeModel.getName());
							empMap.put("positionName",employeeModel.getPositionName());
							empMap.put("figurePhoto",employeeModel.getFigurePhoto());
							emList.add(empMap);
						}
					}
					saveMap.put("empList",emList);
					savemList.add(saveMap);
				}
			}
		}
		return R.ok().put("data", savemList);
	}



}
